package com.app.utils.spring.interceptor;

import com.app.utils.base.http.ResponseHandler;
import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.Logger;
import com.app.utils.spring.config.SystemError;
import com.app.utils.spring.properties.Config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class ExceptionResolver implements HandlerExceptionResolver {
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception ex) {
		if(ex.getCause() instanceof SystemError){
            request.setAttribute("duoke.respType", "other");
            SystemError error = (SystemError)ex.getCause();
            ResponseHandler.write(ResultUtil.error(error.getErrorCode(), error.getMessage()), request, response);
        }else{
            Logger.error("访问异常",ex);
            request.setAttribute("duoke.respType", "error");
            ResponseHandler.write(ResultUtil.error("6999", Config.pp.get("6999")), request, response);
        }
		return new ModelAndView();
	}
	
}
